package com.svms.movement;

import java.text.SimpleDateFormat;
import java.util.Date;

import org.zkoss.zk.ui.sys.ComponentsCtrl;
import org.zkoss.zul.Listcell;
import org.zkoss.zul.Listitem;
import org.zkoss.zul.ListitemRenderer;

import com.svms.service.vo.MovementVO;

public class MovementListModelItemRender implements
		ListitemRenderer<MovementVO> {

	@Override
	public void render(Listitem item, MovementVO data, int index)
			throws Exception {

		final MovementVO movement = (MovementVO) data;

		Listcell lc = new Listcell(movement.getVehicleVO() != null ? movement
				.getVehicleVO().getVehicleNumber() : "NA");
		lc.setParent(item);
		lc = new Listcell(formatDate(movement.getStartDate()));
		lc.setParent(item);
		lc = new Listcell(formatDate(movement.getEndDate()));
		lc.setParent(item);
		lc = new Listcell(movement.getStartPlace());
		lc.setParent(item);
		lc = new Listcell(movement.getDistanceTraveled());
		lc.setParent(item);

		item.setAttribute("selectedMovement", data);
		
		ComponentsCtrl.applyForward(item,
				"onDoubleClick=onMovementItemDoubleClicked");

	}

	private String formatDate(Date date) {
		String formatedDate = null;
		if (date != null) {
			SimpleDateFormat dateFormat = new SimpleDateFormat("dd-MMM-yyyy");
			formatedDate = dateFormat.format(date);
		}
		return formatedDate;
	}
}
